<template>
  <div class="find-page">
    <div class="find-title">
      <div class="back" @click="$router.back()"><i class="iconfont icon-zuojiantou"></i></div>
      <div class="search">
        <div class="left">
          <div class="location" @click="$router.push('/city')">
            {{ cityNow }}<i class="iconfont icon-xiajiantou"></i>
          </div>
          <van-search v-model="value" placeholder="请输入小区或地址" />
        </div>
      </div>
      <div class="map" @click="$router.push('/map')"><i class="iconfont icon-ditu"></i></div>
    </div>
  <van-dropdown-menu active-color="#21b97a">
  <Mymenu title="区域" :conditions="con_area" /> 
  <Mymenu title="方式" :conditions="con_rentType" /> 
  <Mymenu title="租金" :conditions="con_price" /> 
  <Myfilter title="筛选" />
</van-dropdown-menu>
<!-- <HouseItem /> -->
  </div>
</template>

<script>
import Mymenu from '@/components/Mymenu.vue'
import Myfilter from '@/components/Myfilter.vue'
import HouseItem from '@/components/House_Item.vue'
import { searchCommunity, getCityId, searchConditions } from '@/api/search'
export default {
  name: "find-page",
  components:{
    HouseItem,
    Mymenu,
    Myfilter
  },
  data(){
    return {
      value:'',
      cityNow:'上海',
      cityid:'',
      con_area: [], //区域数
      con_rentType: [], //方式数据
      con_price: [], //价格数据
    }
  },
  async created(){
    this.cityNow = localStorage.getItem('cityNow');
    this.getcityNow()
    await this.getcityid(this.cityNow)
    this.getCondition()
   

  },
  methods:{
    // async searchItem(data){
    //   const res = await searchCommunity(data)
    //   console.log(res);
    // },
    async getcityid(name){
       const {body} = await getCityId(name)
       this.cityid = body.value
    },
    getcityNow(){
      this.cityNow = localStorage.getItem("cityNow") || '上海';
    },
    // 房屋查询条件
    async getCondition(){
      const {body} = await searchConditions(this.cityid)
      // console.log(body);
      this.getAllConditions(body)
      // this.con_area = [body.area,body.subway]
      // console.log(this.checkChildren(body.area));
      // console.log(this.con_area);

    },
    getAllConditions(body){
      console.log(body);
      let {area, characteristic, floor, oriented, price, rentType, roomType, subway} = body
      area = this.checkChildren(area)
      subway = this.checkChildren(subway)
      this.con_area = [area,subway]
      this.con_rentType = rentType
      this.con_price = price
      
    },
    checkChildren(obj){
      const checked = Object.prototype.hasOwnProperty.call(obj, 'children')
      if(checked) {
        for(let k in obj.children){
          this.checkChildren(obj.children[k])
        } 
      } else {
          obj.children = [{label:'不限', value:'null'}]
      }
      return obj
    }
  }
};
</script>

<style lang="less" scoped>
.find-title {
  display: flex;
  align-items: center;
  height: 50px;
  background-color: #21b97a;
  .back{
    width: 40px;
    text-align: center;
    color: #fff;
  }
}
.van-search {
  width: 240px;
}
.search {
  width: 280px;
  display: flex;
  margin-right: 8px;
  background-color: #fff;
  height: 34px;
  border-radius: 5px;
  overflow: hidden;
  .left {
    // flex: 1;
    display: flex;
    .location {
      display: flex;
      width: 43px;
      padding-left: 7px;
      font-size: 14px;
      line-height: 34px;
    }
  }
  .map {
    width: 34px;
    height: 34px;
  }
}
.icon-ditu {
  font-size: 33px;
  color: #fff;
}
.icon-xiajiantou {
  font-size: 10px;
  color: #787d82;
  margin-left: 3px;
}
</style>
